﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BUS;
namespace CungMuaC5.Account_Company
{
    public partial class SuaThongTinDN : System.Web.UI.Page
    {
        private DTO_TaiKhoan CurrentUser;
        private BUS_DoanhNghiep bus_DN = new BUS_DoanhNghiep();
        private BUS_LoaiKM bus_loaiKM = new BUS_LoaiKM();
        private BUS_KhuVuc bus_KV = new BUS_KhuVuc();
        private BUS_TaiKhoan bus_tk = new BUS_TaiKhoan();
        protected void Page_Load(object sender, EventArgs e)
        {
            CurrentUser = Session.GetCurrentUser();
            if (!IsPostBack)
            {                
                int maTK = int.Parse(CurrentUser.MaTK.ToString());
                List<DTO_ChiTietDoanhNghiep> chitiet = bus_DN.LayChiTietDN(maTK);
                dvThongTinDN.DataSource = chitiet;
                dvThongTinDN.DataBind();
                List<string> danhsachloai = bus_loaiKM.selectALl();
                DropDownList ddlLHKD = (DropDownList)dvThongTinDN.FooterRow.FindControl("ddlLoaiHinhKD");
                for (int i = 0; i < danhsachloai.Count; i++)
                {
                    ddlLHKD.Items.Add(danhsachloai[i]);
                }
                DropDownList ddlNoiQC = (DropDownList)dvThongTinDN.FooterRow.FindControl("ddlNoiQuangCao");
                List<string> danhsachKV = bus_KV.SelectAllKV();
                for (int i = 0; i < danhsachKV.Count; i++)
                {
                    ddlNoiQC.Items.Add(danhsachKV[i]);
                }
            }
        }

        protected void btSave_Click(object sender, ImageClickEventArgs e)
        {
            //Update thong tin
            TextBox tbName = (TextBox)dvThongTinDN.FooterRow.FindControl("tbName");
            TextBox tbChucVu = (TextBox)dvThongTinDN.FooterRow.FindControl("tbChucVu");
            TextBox tbTenDN = (TextBox)dvThongTinDN.FooterRow.FindControl("tbTenDN");
            TextBox tbWebsite = (TextBox)dvThongTinDN.FooterRow.FindControl("tbWebsite");
            TextBox tbDienThoai = (TextBox)dvThongTinDN.FooterRow.FindControl("tbDienThoai");
            TextBox tbDiaChi = (TextBox)dvThongTinDN.FooterRow.FindControl("tbDiaChi");
            DropDownList ddlLHKD = (DropDownList)dvThongTinDN.FooterRow.FindControl("ddlLoaiHinhKD");
            DropDownList ddlNoiQC = (DropDownList)dvThongTinDN.FooterRow.FindControl("ddlNoiQuangCao");
            TextBox tbHopTac = (TextBox)dvThongTinDN.FooterRow.FindControl("tbHopTac");
            int maTK = int.Parse(CurrentUser.MaTK.ToString());
            DTO_TaiKhoan tk = Session.GetCurrentUser();
            List<DTO_DoanhNghiep> dn = bus_DN.LayDN(maTK);
            tk.HoTen = tbName.Text;
            tk.DienThoai = tbDienThoai.Text;
            dn[0].ChucVu = tbChucVu.Text;
            dn[0].TenDoanhNghiep = tbTenDN.Text;
            dn[0].Website = tbWebsite.Text;
            dn[0].DiaChi = tbDiaChi.Text;
            dn[0].LoaiHinhKinhDoanh = ddlLHKD.SelectedIndex+1;
            dn[0].NoiMuonQuangCao = ddlNoiQC.SelectedIndex + 1;
            dn[0].ThongTinHopTac = tbHopTac.Text;
            bus_DN.Update(dn[0]);
            bus_tk.Update(CurrentUser);
            Response.Redirect("~/Account_Company/ThongTinDN.aspx");
        }
    }
}
